﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Security.Cryptography;

namespace MNNUTEM
{
   
    public partial class Usuarios : Form
    {

        private crudUsuario cUsuario;
        int sucursal=0;
        int rutUsuario, sw = 0;
       
        
        public Usuarios()
        {
            InitializeComponent();
            cUsuario = new crudUsuario();
           
        }

        private void updateGridView()
        {
            dataGridView1.DataSource = cUsuario.Consultation("select *from Usuario");
        
        }

        private void button4_Click(object sender, EventArgs e)
        {

            cUsuario.Insert(textBox1, textBox2, textBox3, textBox4, dateTimePicker1.Value.ToShortDateString(), encriptar(textBox8.Text), textBox6, sucursal);
            updateGridView();
        }

        public static string encriptar(string laCadena)
        {
            SHA1CryptoServiceProvider elProveedor = new SHA1CryptoServiceProvider();
            byte[] vectoBytes = System.Text.Encoding.UTF8.GetBytes(laCadena);
            byte[] inArray = elProveedor.ComputeHash(vectoBytes);
            elProveedor.Clear();
            return Convert.ToBase64String(inArray);
        }


        private void Usuarios_Load(object sender, EventArgs e)
        {
          
           dataGridView2.DataSource = cUsuario.Consultation("select *from Sucursal");
           updateGridView();
           
           button3.Enabled = false;
           button1.Enabled = false;
           textBox9.Enabled = false;

        }

        private void dataGridView2_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            sucursal = int.Parse( dataGridView2[0, e.RowIndex].Value.ToString());
            textBox9.Text = sucursal.ToString();
            button4.Enabled = true;
            sw = 1;
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            cUsuario.Delete(rutUsuario);
            updateGridView();
            clean();
        }

        private void getdatos()
        {
            DataSet DS;
            DS = cUsuario.Consultation2("select *from Usuario where rutUsuario='" + rutUsuario + "'");
            textBox1.Text = DS.Tables[0].Rows[0][0].ToString();
            textBox2.Text = DS.Tables[0].Rows[0][1].ToString();
            textBox3.Text = DS.Tables[0].Rows[0][2].ToString();
            textBox4.Text = DS.Tables[0].Rows[0][3].ToString();
            dateTimePicker1.Text = DS.Tables[0].Rows[0][4].ToString();
            textBox9.Text = DS.Tables[0].Rows[0][7].ToString();
            textBox6.Text = DS.Tables[0].Rows[0][6].ToString(); 
           
        
        
        
        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            rutUsuario = int.Parse(dataGridView1[0, e.RowIndex].Value.ToString());
           
            getdatos();
            button1.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cUsuario.Set(textBox1, textBox2, textBox3, textBox4, dateTimePicker1.Value.ToShortDateString(), textBox8, textBox6,int.Parse(textBox9.Text),rutUsuario);
            updateGridView();
        }

        private void clean()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox6.Text = "";
            textBox9.Text = "";
            textBox8.Text = "";
            button1.Enabled = false;
            button3.Enabled = false;
            if(sw==1)
            button4.Enabled = true;
            
        
        }
        private void button2_Click(object sender, EventArgs e)
        {
            clean();
            
        }
    }
}
